package com.fcscs.cservice.web;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.fcscs.cservice.common.CServiceReportConstants;
import com.fcscs.share.domain.nonexport.FolioAttachment;
import com.fcscs.share.domain.nonexport.RCAttachment;
import com.fcscs.share.service.MiscDBManager;
import com.google.gwt.user.client.Window;

@Controller
public class RegistrationCardReportController {
	
	@Autowired
	MiscDBManager miscMgr;
	
	@RequestMapping("/report/registrationCard.do")
	public void registrationCardReportHandler(@RequestParam("rcId") long rcId, HttpServletRequest request, 
			HttpServletResponse response, SessionStatus status) throws IOException, JRException {
		
		RCAttachment att = miscMgr.getRCAttachment(rcId);
		if (att == null) {
			response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		} else {
			response.addHeader("Content-Disposition", "attachment; filename=RC.pdf");
			response.setContentType("application/pdf");
			response.getOutputStream().write(att.getRcPDF());
			response.getOutputStream().flush();
			response.setStatus(HttpServletResponse.SC_OK);
		}
		
	}
	
	@RequestMapping("/report/folio.do")
	public void folioReportHandler(HttpServletRequest request, HttpServletResponse response,
			SessionStatus status) throws IOException, JRException {
		long folioId = Long.valueOf(request.getParameter(CServiceReportConstants.FOLIOID));
		System.out.println("folioId"+folioId);
		
		Window.alert("folioId"+folioId);
		FolioAttachment att = miscMgr.getFolioAttachment(folioId);
		if (att == null) {
			Window.alert("att == null");
			System.out.println("att == null");
			response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		} else {
			Window.alert("else");
			System.out.println("else");
			response.addHeader("Content-Disposition", "attachment; filename=folio.pdf");
			response.setContentType("application/pdf");
			response.getOutputStream().write(att.getFolioPDF());
			response.getOutputStream().flush();
			response.setStatus(HttpServletResponse.SC_OK);
		}
	}



}
